package com.wuyu.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {
    private static final String SING = "!Q@W3e4r%T^Y";

    public static String getToken(Map<String,String> map){
        // 获取当前时间
        Calendar instance = Calendar.getInstance();
        // Calendar中add，对天数进行操作，设置当前时间后七天
        instance.add(Calendar.DATE,7);
        // 创建JWT builder
        JWTCreator.Builder builder = JWT.create();
        // 设置payload
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder
                .withExpiresAt(instance.getTime())//指定令牌过期时间
                .sign(Algorithm.HMAC256(SING));
        System.out.println(token);
        return token;
    }

    /**
     *  获取token信息
     * */
    public static DecodedJWT getTokenInfo(String token){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        // 从DecodedJWT中拿token的信息
        return decodedJWT;
    }
}
